package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private EmpService empService;


    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<Clazz> getPageResult(ClazzQueryParam param) {
        Page<Clazz> page = new Page<>(param.getPage(), param.getPageSize());

        //根据最后修改时间倒序排序
        page.addOrder(OrderItem.desc("update_time"));

        //构建条件查询
        LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(param.getName() != null && !param.getName().isEmpty(), Clazz::getName, param.getName())
                .between(param.getBegin() != null && param.getEnd() != null, Clazz::getEndDate, param.getBegin(),  param.getEnd());

        //调用page方法完成分页查询
        page = page(page ,queryWrapper);


            //获取员工集合
            List<Emp> emps = empService.list();
            Map<Long, String> empMap = emps.stream()
                    .collect(Collectors.toMap(Emp::getId, Emp::getName));
            List<Clazz> clazzs = page.getRecords().stream()
                    .peek(clazz -> clazz.setMasterName(empMap.get(clazz.getMasterId())))
                    .toList();
            // 获取开班状态并设置到每个Clazz对象中
            clazzs.forEach(clazz -> {
                LocalDate now = LocalDate.now();
                if (now.isAfter(clazz.getBeginDate()) && now.isBefore(clazz.getEndDate())) {
                    clazz.setStatus("开班中");
                } else if (now.isBefore(clazz.getBeginDate())) {
                    clazz.setStatus("未开班");
                } else {
                    clazz.setStatus("已结班");
                }
            });



        PageResult<Clazz> pageResult = new PageResult<>(page.getTotal(), clazzs);


        //封装结果并返回
        return pageResult;
    }

    /**
     * 新增班级
     *
     * @param clazz
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        save(clazz);

    }

    /**
     * 根据id查询班级
     *
     * @param id
     * @return
     */
    @Override
    public Clazz getClazzById(Integer id) {
        Clazz clazz = getById(id);
        return clazz;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        updateById(clazz);
    }

}
